package com.jml.zlhq.app.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Base64;

public class SharedPreferenceHelper {
	private Context context;
	SharedPreferences helper;
	Editor editor;

	public SharedPreferenceHelper(Context context, String tableName) {
		this.context = context;
		helper = context.getSharedPreferences(tableName, Context.MODE_PRIVATE);
		editor = helper.edit();
	}

	public void saveData(String tableName, Map<String, String> datas) {

		if (TextUtils.isEmpty(tableName)) {
			helper = PreferenceManager.getDefaultSharedPreferences(context);
		} else {
			helper = context.getSharedPreferences(tableName,
					Context.MODE_PRIVATE);
		}

		editor = helper.edit();

		Set<Map.Entry<String, String>> dataSet = datas.entrySet();
		for (Iterator<Map.Entry<String, String>> it = dataSet.iterator(); it
				.hasNext();) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) it
					.next();
			editor.putString(entry.getKey(), entry.getValue());
		}

		editor.commit();

		// for (Iterator<Map.Entry<String, String>> it = dataSet.iterator(); it
		// .hasNext();) {
		// @SuppressWarnings("unchecked")
		// Map<String, String> entry = (Map<String, String>) it
		// .next();
		//
		// Iterator<String> i = entry.keySet().iterator();
		// while (i.hasNext())
		// {
		// String key = i.next();
		// System.out.println();
		// }
		// }

	}

	public String getData(String tableName, String key, String value) {
		if (TextUtils.isEmpty(tableName)) {
			helper = PreferenceManager.getDefaultSharedPreferences(context);
		} else {
			helper = context.getSharedPreferences(tableName,
					Context.MODE_PRIVATE);
		}
		return helper.getString(key, value);
	}

	public boolean getBooleanValue(String tableName, String key, Boolean value) {

		if (TextUtils.isEmpty(tableName)) {
			helper = PreferenceManager.getDefaultSharedPreferences(context);
		} else {
			helper = context.getSharedPreferences(tableName,
					Context.MODE_PRIVATE);
		}
		return helper.getBoolean(key, value);
	}

	public void saveFlag(String str, String name) {
		editor.putString(str, name);
		editor.commit();
	}

	public String getString(String str) {
		return helper.getString(str, "");
	}

	// �����ݵķ���
	public void saveFlag(int i) {
		editor.putInt("flag", i);
		editor.commit();
	}

	public void saveFlag(String name, int i) {
		editor.putInt(name, i);
		editor.commit();
	}

	public int getFlag() {
		return helper.getInt("flag", -1);
	}

	public int getFlag(String str) {
		return helper.getInt(str, -1);
	}

	@SuppressWarnings("unchecked")
	public static List String2SceneList(String SceneListString)
			throws StreamCorruptedException, IOException,
			ClassNotFoundException {
		byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
				Base64.DEFAULT);
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				mobileBytes);
		ObjectInputStream objectInputStream = new ObjectInputStream(
				byteArrayInputStream);
		List SceneList = (List) objectInputStream.readObject();
		objectInputStream.close();
		return SceneList;
	}

	// SharedPreferences mySharedPreferences= getSharedPreferences("scenelist",
	// Context.MODE_PRIVATE);
	// Editor edit = mySharedPreferences.edit();
	// try {
	// String liststr = Utils.SceneList2String(MyApp.scenesList);
	// edit.putString(Constants.SCENE_LIST,liststr);
	// edit.commit();
	// } catch (IOException e) {
	//
	// e.printStackTrace();
	// }
	//
	//
	//
	// SharedPreferences sharedPreferences= getActivity().getSharedPreference
	// ("scenelist", Context.MODE_PRIVATE);
	// String liststr = sharedPreferences.getString(Constants.SCENE_LIST, "");
	// try {
	// showSceneList = Utils.String2SceneList(liststr);
	// } catch (StreamCorruptedException e) {
	// e.printStackTrace();
	// } catch (IOException e) {
	// e.printStackTrace();
	// } catch (ClassNotFoundException e) {
	// e.printStackTrace();
	// }
}
